﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManageSystem
{
    public partial class ManageStuff : Form
    {

        private string userID;
        public ManageStuff()
        {
            InitializeComponent();
        }

        private void ManageStuff_Load(object sender, EventArgs e)
        {
            initData();
            textBox1.Focus();
        }

        private void initData()
        {
            listView1.Items.Clear();
            SqlDataReader sdr = Utils.getDataReader("select * from UserInfo");
            while (sdr.Read())
            {

                ListViewItem lv = new ListViewItem(sdr[0].ToString());


                lv.SubItems.Add(sdr[3].ToString());
                lv.SubItems.Add(sdr[4].ToString());
                lv.SubItems.Add(sdr[5].ToString());
                lv.SubItems.Add(sdr[7].ToString());
                lv.SubItems.Add(sdr[6].ToString().Substring(0, 10));
                listView1.Items.Add(lv);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {

                this.textBox1.Text = this.listView1.SelectedItems[0].SubItems[1].Text;
                if (this.listView1.SelectedItems[0].SubItems[2].Text == "男")
                    this.radioButton1.Checked = true;
                else
                    this.radioButton2.Checked = true;
                //this.dtpBirth.Value = Convert.ToDateTime(this.listView1.SelectedItems[0].SubItems[3].Text);
                this.textBox2.Text = this.listView1.SelectedItems[0].SubItems[3].Text;
                this.userID = this.listView1.SelectedItems[0].SubItems[0].Text;
                this.textBox3.Text = listView1.SelectedItems[0].SubItems[4].Text;

            }
        }

        private void listView1_Click(object sender, EventArgs e)
        {
            //if (this.listView1.SelectedItems.Count > 0)
            //{

            //    this.textBox1.Text = this.listView1.SelectedItems[0].SubItems[1].Text;
            //    if (this.listView1.SelectedItems[0].SubItems[2].Text == "男")
            //        this.radioButton1.Checked = true;
            //    else
            //        this.radioButton2.Checked = true;
            //    //this.dtpBirth.Value = Convert.ToDateTime(this.listView1.SelectedItems[0].SubItems[3].Text);
            //    this.textBox2.Text = this.listView1.SelectedItems[0].SubItems[3].Text;
            //    this.userID = this.listView1.SelectedItems[0].SubItems[0].Text;

            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                MessageBox.Show("请输入姓名!");
                textBox1.Focus();
                return;
            }
            else if (textBox2.Text.Equals(""))
            {
                MessageBox.Show("请输入年龄！");
                textBox2.Focus();
                return;
            }
            else if (this.textBox2.Text != "")
            {
                if (this.IsNumberic(this.textBox2.Text) == false)

                {
                    MessageBox.Show("年龄请输入数字！");
                    this.textBox2.Focus();
                    return;
                }
            }
            else if (textBox3.Text.Equals(""))
            {
                MessageBox.Show("请输入班级！");
                textBox2.Focus();
                return;
            }

            if (MessageBox.Show("确定要添加用户?", "添加询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                string commend = "select * from UserInfo where UserName='" + this.textBox1.Text + "'";
                SqlDataReader dataReader = Utils.getDataReader(commend);
                if (dataReader.Read())
                {
                    MessageBox.Show("用户名已存在，请修改用户名！");
                    textBox1.Focus();
                    return;
                }
                string stuSex = "";
                if (radioButton1.Checked == true)
                    stuSex = "男";
                else if (radioButton2.Checked == true)
                    stuSex = "女";
                string str = "insert into UserInfo (UserName,UserSex,UserAge,UserClass) values(N'" + textBox1.Text + "',N'" + stuSex + "'," + textBox2.Text + ",N'" + textBox3.Text + "')";
                if (Utils.ExecuteNoQuery(str) != -1)
                {
                    MessageBox.Show("添加成功");
                    ClearText();
                    initData();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }

        }

        private void ClearText()
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private bool IsNumberic(string oText)
        {
            try
            {
                int var1 = Convert.ToInt32(oText);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.userID == "")
            {
                MessageBox.Show("请选择要删除的用户！");
                return;
            }
            if (MessageBox.Show("是否要删除当前选中用户?", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                string delStr = "delete from UserInfo where UserId=" + this.userID;
                Utils.ExecuteNoQuery(delStr);


                //sqlCmd.CommandText = delStr;
                //sqlCmd.Connection = sqlCon;
                //sqlCmd.ExecuteNonQuery();

                this.ClearText();

                this.initData();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                MessageBox.Show("请输入姓名!");
                textBox1.Focus();
                return;
            }
            else if (textBox2.Text.Equals(""))
            {
                MessageBox.Show("请输入年龄！");
                textBox2.Focus();
                return;
            }
            else if (this.textBox2.Text != "")
            {
                if (this.IsNumberic(this.textBox2.Text) == false)

                {
                    MessageBox.Show("年龄请输入数字！");

                    this.textBox2.Focus();
                    return;
                }
            }
            else if (textBox3.Text.Equals(""))
            {
                MessageBox.Show("请输入班级！");
                textBox2.Focus();
                return;
            }

            if (MessageBox.Show("是否要修改当前选中用户?", "修改询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                string commend = "select * from UserInfo where UserName='" + this.textBox1.Text + "'";
                SqlDataReader dataReader = Utils.getDataReader(commend);
                if (dataReader.Read())
                {
                    MessageBox.Show("用户名已存在，请修改用户名！");
                    textBox1.Focus();
                    return;
                }
                string stuSex = "";
                if (radioButton1.Checked == true)
                    stuSex = "男";
                else if (radioButton2.Checked == true)
                    stuSex = "女";
                string str = "update  UserInfo set UserName=N'" + textBox1.Text + "',UserAge=" + textBox2.Text + ",UserSex=N'" + stuSex + "' ,UserClass=N'" + textBox3.Text + "' where UserId=" + userID;
                if (Utils.ExecuteNoQuery(str) != -1)
                {
                    MessageBox.Show("修改成功");
                    initData();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
        }
    }
}
